﻿@{
    Layout = null;
}
@using CNKI.TPI.Web.Admin.Model
<script>
    var myTree;
    function loadTree() {
        myTree = dhtmlXTreeFromHTML("treeboxbox_tree"); // 加载检索树
        myTree.attachEvent("onClick", function (id) {
            selectSingle(id); //选择事件
        });
    }
    function addNode(idstr) {
        var codegrde = idstr;
        var unitid = $('#unitID').val();
        myTree.deleteChildItems(codegrde);//加载之前先删除他的页子结点
        var url =  timestamp("@Html.Raw(Url.Action("GetDeptCodeChilds", "Admin_User"))?codegrde=" + codegrde + "&unitid=" + unitid);//发送AJAX的URL
            $.getJSON(
              url,
              function (data) {
                  $.each(data, function (index, entry) {//ajax回调后,循环.
                      var nodeid = entry['SYSCODE'] + "," + entry['GRADE'] + "," + entry['SYSID'];
                      myTree.insertNewChild(codegrde, nodeid, entry['DEPTNAME'], '', '', '', '', '', '');//使用这个函数.
                      if (entry['HASCHILDFLAG'] == 1)
                      {
                          addNode(nodeid);
                      }
                  });

              }
             );

        }
    function closeDept() {
        $(".talkDivBg").hide();
        $(".form-h-8em").hide();
    }
    function showDept() {
        $(".talkDivBg").show();
        $(".form-h-8em").show();
    }
    $(document).ready(function () {
        loadTree();

    });
    function selectSingle(codegrade) {
        $("#DeptCodeGrade").val(codegrade);
    }
    function addnextdept() {
        if (isSelectnode()) {
            $("#deptname").val("");
            $("#Flage").val(1);//标识增加下级节点
            $(".talkDiv-title span").html("增加下级部门");
            showDept();
        }
    }
    function addfirstdept() {
        $("#deptname").val("");
        $("#Flage").val(0);//标识增加一级节点
        $(".talkDiv-title span").html("增加一级部门");
        showDept();

    }
    function savedept() {
        var unitid = $('#unitID').val();
        var name = $('#deptname').val();
        if ($.trim(name) == "" ) {
            messageAlert("请输入部门名称！");
            return;
        }
        else {
            if (!regtext(name)) {
                messageAlert("请输入部门名称(不允许输入特殊字符)！");
                return;
            }
        }
        var flage = $("#Flage").val();
        var sysid = 0;
        var grade = 0;
        var parentcode = "000";//code ,grade,id
        var codestr = $("#DeptCodeGrade").val();
        if (flage != "0") {
            var arry = codestr.split(',');
            if (flage == 2) {  //更新时sysid赋值，新增时sysid为0
                sysid = arry[2];
            }
            parentcode = arry[0];
            grade = arry[1];
        }
        var data = { "unitid": unitid, "sysid": sysid, "deptName": name, "parentcode": parentcode, "grade": grade };
        var delurl = "@Html.Raw(Url.Action("SaveDeptInfo", "Admin_User"))";
        dealdata(delurl, data);
        closeDept();
        if (flage == 1) {
            //ajaxLoad("initload", '@Html.Raw(Url.Action("DeptManager", "Admin_User"))?unitid=' + unitid);
            addNode(codestr);//新增下级节点后加载节点下数据
        }
        else if (flage == 2) {  //更新成功后，刷新节点数据

            //设置节点名称
            myTree.setItemText(codestr, name, name);
        }
        else if (flage == 0) { //一级节点增加完成后，刷新tree
            ajaxLoad("initload", '@Html.Raw(Url.Action("DeptManager", "Admin_User"))?unitid=' + unitid);
        }
    closeDept();
}
function isSelectnode() {
    var parentcode = $("#DeptCodeGrade").val();
    if (parentcode == 0) {
        messageAlert("请选择节点！");
        return false;
    }
    else {
        return true;
    }
}
function updatedept() {
    if (isSelectnode()) {
        $("#Flage").val(2);//标识更新
        $(".talkDiv-title span").html("更新部门");
        var nodeid = $("#DeptCodeGrade").val();
        var name = myTree.getItemText(nodeid);
     
        $("#deptname").val(name);
        showDept();

    }
}
function deletedept() {
    if (isSelectnode()) {
        confirmDialog('确定要删除吗?', function () {
            var str = $("#DeptCodeGrade").val();
            var delurl = "@Html.Raw(Url.Action("DeleteDept", "Admin_User"))";
            var data = { "codegradeid": str, "unitid": $('#unitID').val() };
            dealdata(delurl, data);
            closeDept();
            myTree.deleteItem(str, true);
            $("#DeptCodeGrade").val(0);//清空选择的节点
            myTree.clearSelection();
        })
    }
}
//调用controll
function dealdata(urlstr, datastr) {
    var unitid = $('#unitID').val();
    $.ajax({
        type: "POST",
        url: urlstr,
        data: datastr,
        success: function (m) {
            if (m == "True") {
                $("#DeptCodeGrade").val(0);//清空选择的节点
                myTree.clearSelection();
                messageAlert("操作成功！");
            }
            else {
                messageAlert("操作失败！");
            }
            enablesave();
        },
        error: function (e)
        { enablesave(); }
    })
}
function disablesave() {
    $(".del").attr("disabled", "disabled");
}
function enablesave() {
    $(".del").removeAttr("disabled", "disabled");
}
</script>
<style>
span.selectedTreeRow {
    background-color:#e6e6e6  !important;
    width: 250px !important;
    }
   div{ cursor:default }
</style>
<div class="wrapR-c" style="margin-top: 10px">
    <h3 class='box-h'>部门管理</h3>
    <input hidden="hidden" value="@ViewBag.UnitID" id="unitID" type="hidden" />
    <input hidden="hidden" value="0" id="DeptCodeGrade"  type="hidden"/>
    <input hidden="hidden" value="0" id="Flage" type="hidden"/>
    <div class='posr' style="margin-bottom: 20px; padding-top: 20px">
        <button class="btn btn-m btn-submit btn-defa-f" onclick="addfirstdept()">增加一级部门</button>
        <button class="btn btn-m btn-submit btn-defa-f" onclick="addnextdept()">增加下级部门</button>
        <button class="btn btn-m btn-submit btn-defa-f" onclick="updatedept()">更新部门</button>
        <button class="btn btn-m btn-submit btn-defa-f del" onclick="deletedept()">删除部门</button>

        <button  class="btn btn-m btn-prim-f" onclick="ajaxLoad('initload', '@Html.Raw(Url.Action("UnitIndex", "Admin_User"))');">返回</button>
    </div>
    <div id="treeboxbox_tree" enabletreeimages="1" enableautotooltips="1" enablemultilineitems="1"
        setimagepath="~/Content/dhtmlxTree/codebase/imgs/dhxtree_skyblue/">
        <ul>
            @{ 
                if (ViewBag.DeptList != null)
                {

                    int preFlag = 0;
                    foreach (SYS_CMS_USER_UNIT_DEPT item in ViewBag.DeptList)
                    {
                        if (item.GRADE < preFlag)
                        {
                            for (int i = 0; i < preFlag - item.GRADE; i++)
                            {
                @Html.Raw("</ul></li>");
                            }
                        }
                        preFlag = item.GRADE;
                        var idstr = item.SYSCODE + "," + item.GRADE + "," + item.SYSID;
                @Html.Raw("<li id=" + idstr + ">" + item.DEPTNAME);
                                                                  if (1 == item.HASCHILDFLAG)
                                                                  {
                @Html.Raw("<ul>");
                                                                  }
                                                                  else
                                                                  {
    
                @Html.Raw("</li>");
                                                                  }
                    }
                    for (int i = 0; i < preFlag - 1; i++)
                    {
                @Html.Raw("</ul></li>");
                    }
                }
              
            }
        </ul>
        <ul>
        </ul>
    </div>

</div>

  <div class="talkDivBg" style="display: none;">
    <!--talkDiv弹出层(不带图片，宽550)-->
    <div class="talkDiv talkW-550 form-h form-h-8em" style="display: none">
        <div class="talkDiv-title">
            <span>添加部门</span>
            <a class="icon icon-close" title="关闭" href="javascript:void(0);" onclick="closeDept()"><i class="iconfont icon-error3"></i></a>
        </div>
        <div class="talkDiv-body talkDiv-body1">

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">部门名称：</label>
                </div>
                <div class="f-ct">
                    <input type="text" class="f-text span4" id="deptname" maxlength="20"> <span class="f-label-re">*</span>
                </div>
            </div>


        </div>
        <div class="talkDiv-btn"><a href="javascript:void(0);" class="btn btn-m btn-prim-f btn-submit" onclick="savedept()">保存</a> </div>
    </div>
</div>

